package org.xt.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.trans.Atom;
import org.nutz.trans.Trans;
import org.xt.entity.Permission;
import org.xt.entity.Role;
import org.xt.entity.RolePermission;

@IocBean(name="roleService")
public class RoleService {
	@Inject
	private Dao dao;
	
	public Role add(Role role){
		role.setUpdateTime(new Date());
		role.setCreateTime(new Date());
		return dao.insert(role);
	}
	public int delete(int id){
		Trans.exec(new Atom() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				dao.delete(Role.class, id);
				Sql sql = Sqls.create("DELETE FROM tb_role_permission WHERE roleId=@id");
				sql.params().set("id", id);
				Sql sql1 = Sqls.create("DELETE FROM tb_user_role WHERE roleId=@id");
				sql1.params().set("id", id);
				dao.execute(sql1);
			}
		});
		return 1;
	}
	public QueryResult findPermissionsNotRole(int roleId, String serach, int page, int size){
		List<RolePermission> rps = dao.query(RolePermission.class, Cnd.where("roleId", "=", roleId));
		if(rps!= null && rps.size() > 0){
			int[] pids = new int[rps.size()];
			for(int i = 0; i < rps.size(); i ++){
				pids[i] = rps.get(i).getPermissionId();
			}
			Criteria cri = Cnd.cri();
			
			cri.where().andLike("descript", serach).andNotIn("id", pids);
			Pager pager = dao.createPager(page, size);
			pager.setRecordCount(dao.count(Permission.class, cri));
			List<Permission> ps = dao.query(Permission.class,cri, pager);
			return new QueryResult(ps, pager);
		}else{
			Pager pager = dao.createPager(page, size);
			pager.setRecordCount(dao.count(Permission.class, Cnd.where("descript", "like", "%" + serach + "%")));
			List<Permission> ps = dao.query(Permission.class, Cnd.where("descript", "like", "%" + serach + "%"), pager);
			return new QueryResult(ps, pager);
		}
		
	}
	public List<Permission> findPermissionsByRole(int roleId){
		List<RolePermission> rps = dao.query(RolePermission.class, Cnd.where("roleId", "=", roleId));
		if(rps!= null && rps.size() > 0){
			List<Permission> ps = new ArrayList<>();
			for(int i = 0; i < rps.size(); i ++){
				ps.add(dao.fetch(Permission.class, rps.get(i).getPermissionId()));
			}
			return ps;
		}
		return null;
	}
	public int addPermission(int roleId, int... permissIds){
		int sum = 0;
		for(int pid: permissIds){
			if(dao.fetch(Role.class, roleId) == null || dao.fetch(Permission.class, pid) == null){
				continue;
			}
			RolePermission rp = new RolePermission();
			rp.setRoleId(roleId);
			rp.setPermissionId(pid);
			rp.setCreateTime(new Date());
			rp.setUpdateTime(new Date());
			try{
				rp = dao.insert(rp);
				if(rp != null){
					sum ++;
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return sum;
	}
	public int removePermission(int roleId, int... permissIds){
		int sum = 0;
		for(int pid : permissIds){
			sum += dao.deletex(RolePermission.class, roleId, pid);
		}
		return sum;
	}
}
